from jili.trade.constants import product


class matrix_count_bybar:
    def __init__(self, keys,mode=None):
        self.d=keys
        self.total=0
        if mode=="buy":
            self.oncalc=self.oncalc_buy
        elif mode=="sell":
            self.oncalc=self.oncalc_sell
        elif mode=="long":
            self.oncalc = self.oncalc_None_long
        elif mode=="short":
            self.oncalc=self.oncalc_None_short
        else:
            self.oncalc=self.oncalc_None
        for i in self.d:
            for j in self.d:
                if i < j:
                    self.total=self.total+1
    def oncalc_buy(self,b,timekey=None):
        if b[self.d[-1]]:
            n=0
            for i in self.d:
                for j in self.d:
                    if i<j:
                        if b[i]>b[j]:
                            n=n+1
            return n/self.total
        else:
            return None
    def oncalc_sell(self,b,timekey=None):
        if b[self.d[-1]]:
            n=0
            for i in self.d:
                for j in self.d:
                    if i<j:
                        if b[i]<b[j]:
                            n=n-1
            return n/self.total
        else:
            return None
    def oncalc_None(self,b,timekey=None):
        if b[self.d[-1]]:
            n=0
            for i in self.d:
                for j in self.d:
                    if i<j:

                        if b[i]>b[j]:
                            n=n+1
                        else:
                            n=n-1
            return n/self.total
        else:
            return None
    def oncalc_None_short(self,b,timekey=None):
        if b[self.d[-1]]:
            n=0
            for i in self.d:
                for j in self.d:
                    if i<j:

                        if b[i]>b[j]:
                            n=n+1
                        else:
                            n=n-1
            if n>0:
                n=0
            return n/self.total
        else:
            return None

    def oncalc_None_long(self, b, timekey=None):
        if b[self.d[-1]]:
            n = 0
            for i in self.d:
                for j in self.d:
                    if i < j:

                        if b[i] > b[j]:
                            n = n + 1
                        else:
                            n = n - 1
            if n<0:
                n=0
            return n / self.total
        else:
            return None

class matrix_count_up_bybar:
    def __init__(self, keys):
        self.d=keys
        self.total=0
        for i in self.d:
            for j in self.d:
                if i < j:
                    self.total=self.total+1
    def oncalc(self,b,timekey=None):
        if b[self.d[-1]]:
            n=0
            for i in self.d:
                for j in self.d:
                    if i<j:
                        if b[i]>b[j]:
                            n=n+1
            return n/self.total
        else:
            return None
if __name__=="__main__":
    from research.calcor.calcor_show import calcor_show

    args=[2,3,4,5,6]
    keys2 = []
    cc=[]
    for n in args:
        out = "MA" + str(n)
        keys2.append(out)
        cc.append({'calc_cmd': 'ta', 'cmd': 'SMA', 'out': [out], 'input': {'price': "close"}, 'arg': {'timeperiod': n},
                   'batch': n, "fkey": "SMA", "fname": ["简单平均线"], "ftype": []}  # batch:timeperiod 无效值：None np.nan
                  )
    cc = cc + [
        {'calc_cmd': 'class', "class": "matrix_count_up", "out": ["pos_r_1"],
         "init_args": {"keys": keys2}},
        {'calc_cmd': 'class', "class": "matrix_count", "out": ["pos_r_2"],
         "init_args": {"keys": keys2}},
        {'calc_cmd': 'class', "class": "matrix_count", "out": ["pos_r_2buy"],
         "init_args": {"keys": keys2,"fx":"buy"}},
        {'calc_cmd': 'class', "class": "matrix_count", "out": ["pos_r_2sell"],
         "init_args": {"keys": keys2,"fx":"sell"}}
    ]
    calcor_show(obj="601919", start="20240103", end="20240731", cc=cc, factors=[], prebatch=10, main_names=keys2,
                sub_names=[("line", ["pos_r_1"]),("line", ["pos_r_2buy"])]
                ,baseurl=r"G:\price_rq0",factor_url=r"G:\factor",product="stock",datatype="k1d",fqtype="hfq",source=None,region="C")